function initFieldConfig(config){
    var form = Ext.create('Ext.form.Panel', {
        width: '100%',
        height: '100%',
        itemId: 'window_zdpz_contentForm',
        layout: 'column',
        defaultType: 'textfield',
        scrollable:true,
        defaults: {
            margin: '5 5 2 5',
            columnWidth: .99,
            labelAlign: "right",
            labelWidth: 110//控件默认标签宽度
        },
        items: [
                {
                	xtype: 'combobox',
				    fieldLabel: '数据列',
				    name: 'FIELD_CODE',
				    displayField: 'name',
                    valueField: 'id',
                    store: fieldCodeStore(q_id),
                    editable: false,
				    allowBlank: true,
				    enableKeyEvents:true,
				    hidden:config.colCountFlag,
				    listeners:{
				    	select:function ( combo, record, eOpts ){
				    		if(config.saveFlag){
				    			document.getElementById("reportManageGridIframe").contentWindow
		    					.setCellValue(config.row+config.rowCount,config.col,record.get("code"));
				    			document.getElementById("reportManageGridIframe").contentWindow
		    					.lockCell(config.row+config.rowCount,config.col);
				    			fieldConfig(config.f_id);
				    		}
				    	},
	            		 change : function( self, newValue, oldValue, eOpts ){
	            			 if(config.saveFlag){
					    			document.getElementById("reportManageGridIframe").contentWindow
			    					.setCellValue(config.row+config.rowCount,config.col,newValue);
					    			document.getElementById("reportManageGridIframe").contentWindow
			    					.lockCell(config.row+config.rowCount,config.col);
					    			fieldConfig(config.f_id);
					    		}
					     }
				    }
				},
				{
				    fieldLabel: '<span class="required">✶</span>数据列名称',
				    name: 'FIELD_TITLE',
				    allowBlank: false,
				    listeners:{
				    	change : function( self, newValue, oldValue, eOpts ){
		    				if(config.saveFlag){
		    					document.getElementById("reportManageGridIframe").contentWindow
		    					.setCellValue(config.row,config.col,newValue);
				    			saveFieldFormValues(config.f_id);
				    		}
				    	}
				    }
				},
				{
					xtype: 'combobox',
				    fieldLabel: '<span class="required">✶</span>数据列类型',
				    name: 'FIELD_TYPE',
				    displayField: 'name',
                    valueField: 'id',
                    value: 'string',
                    store: DebtEleStore(json_field_type),
                    editable: false,
				    allowBlank: false,
				    hidden:config.colCountFlag,
				    listeners:{
				    	change : function( self, newValue, oldValue, eOpts ){
				    		if(config.saveFlag){
				    			saveFieldFormValues(config.f_id);
				    		}
				    		if(newValue == "string" || newValue == "rstring" || newValue == null){
				    			var isSum = Ext.ComponentQuery.query('combobox[name="IS_SUM"]')[0];
				    			if(isSum){
				    				isSum.setValue('0');
					    			isSum.setHidden(true);
				    			}
				    		}else{
				    			var isSum = Ext.ComponentQuery.query('combobox[name="IS_SUM"]')[0];
				    			if(isSum){
					    			isSum.setHidden(false);
				    			}
				    		}
				    	}
				    }
				},
				{
				    fieldLabel: '超级链接',
				    name: 'DATA_URL',
				    allowBlank: true,//允许为空
				    hidden:config.colCountFlag,
				    listeners:{
				    	change : function( self, newValue, oldValue, eOpts ){
				    		if(config.saveFlag){
				    			saveFieldFormValues(config.f_id);
				    		}
				    	}
				    }
				},
				{
					xtype: 'combobox',
				    fieldLabel: '超级链接数据列',
				    name: 'DATA_URL_FIELD',
				    displayField: 'text',
                    valueField: 'id',
                    store: Ext.ComponentQuery.query('treepanel#fieldTree')[0].getStore(),
                    editable: false,
				    allowBlank: true,//允许为空
				    hidden:config.colCountFlag,
				    listeners:{
				    	change : function( self, newValue, oldValue, eOpts ){
				    		if(config.saveFlag){
				    			saveFieldFormValues(config.f_id);
				    		}
				    	}
				    }
				},
				{
					xtype: 'combobox',
				    fieldLabel: '链接打开方式',
				    name: 'URL_TYPE',
				    displayField: 'name',
				    valueField: 'id',
                    store: DebtEleStore(json_url_type),
                    editable: false,
				    allowBlank: true,//允许为空
				    hidden:config.colCountFlag,
				    listeners:{
				    	change : function( self, newValue, oldValue, eOpts ){
				    		if(config.saveFlag){
				    			saveFieldFormValues(config.f_id);
				    		}
				    	}
				    }
				},
				{
					xtype: 'combobox',
				    fieldLabel: '<span class="required">✶</span>列是否可见',
				    name: 'IS_SHOW',
				    displayField: 'name',
				    valueField: 'id',
				    value:1,
                    store: DebtEleStore(json_show_type),
                    editable: false,
				    allowBlank: false,//允许为空
				    hidden:config.colCountFlag,
				    listeners:{
				    	change : function( self, newValue, oldValue, eOpts ){
				    		if(config.saveFlag){
				    			saveFieldFormValues(config.f_id);
				    		}
				    	}
				    }
				},
				{
					xtype: 'combobox',
				    fieldLabel: '<span class="required">✶</span>是否超链接参数',
				    name: 'IS_URL_PARAM',
				    displayField: 'name',
				    valueField: 'id',
				    value:0,
				    store: DebtEleStore(json_url_param_type),
				    editable: false,
				    allowBlank: false,//允许为空
				    hidden:config.colCountFlag,
				    listeners:{
				    	change : function( self, newValue, oldValue, eOpts ){
				    		if(config.saveFlag){
				    			saveFieldFormValues(config.f_id);
				    		}
				    	}
				    }
				},
				{
					xtype: 'combobox',
				    fieldLabel: '<span class="required">✶</span>是否汇总列',
				    name: 'IS_SUM',
				    displayField: 'name',
				    valueField: 'id',
				    value:0,
				    hidden:true,
				    store: DebtEleStore(json_sum_type),
				    editable: false,
				    allowBlank: false,//允许为空
				    listeners:{
				    	change : function( self, newValue, oldValue, eOpts ){
				    		if(config.saveFlag){
				    			saveFieldFormValues(config.f_id);
				    		}
				    	}
				    }
				},
				{
					xtype: 'combobox',
				    fieldLabel: '<span class="required">✶</span>列是否自动换行',
				    name: 'IS_NEWLINE',
				    displayField: 'name',
				    valueField: 'id',
				    value:0,
				    store: DebtEleStore(json_newline_type),
				    editable: false,
				    allowBlank: false,//允许为空
				    hidden:config.colCountFlag,
				    listeners:{
				    	change : function( self, newValue, oldValue, eOpts ){
				    		if(config.saveFlag){
				    			saveFieldFormValues(config.f_id);
				    		}
				    	}
				    }
				}
        ]
    });
    //初始化及回显configColumns
    form.getForm().setValues(config.data);
    setTimeout(function () {  
    	config.saveFlag=true;
    }, 1000);  
    return form;
}

function saveFieldFormValues(f_id){
	changeFlag = true;
	var form=Ext.ComponentQuery.query('form#window_zdpz_contentForm')[0];
	var formValues = form.getForm().getValues();
	for(var i = 0; i < configColumns.length; i++){
		if(configColumns[i].F_ID == f_id ){
			configColumns[i].FIELD_CODE = formValues.FIELD_CODE;
			configColumns[i].FIELD_TITLE = formValues.FIELD_TITLE;
			configColumns[i].FIELD_TYPE = formValues.FIELD_TYPE;
			configColumns[i].DATA_URL = formValues.DATA_URL;
			configColumns[i].DATA_URL_FIELD = formValues.DATA_URL_FIELD;
			configColumns[i].URL_TYPE = formValues.URL_TYPE;
			configColumns[i].IS_URL_PARAM = formValues.IS_URL_PARAM;
			configColumns[i].IS_SHOW = formValues.IS_SHOW;
			configColumns[i].IS_SUM = formValues.IS_SUM;
			configColumns[i].IS_NEWLINE = formValues.IS_NEWLINE;
			break;
		}
	}
}

function fieldConfig(f_id){
	var form=Ext.ComponentQuery.query('form#window_zdpz_contentForm')[0];
	var fieldCode = Ext.ComponentQuery.query('combobox[name="FIELD_CODE"]')[0].getValue();
	Ext.Ajax.request({
		method : 'POST',
		url : "/sys-report/getFieldConfigByCode",
		params : {
			fieldCode: fieldCode
		},
		async : false,
		success : function(response, options) {
//			// 获取返回的JSON，并根据gridid，获取表头配置信息
			var respText = Ext.util.JSON.decode(response.responseText);
			if(respText.success){
				var fieldCodeConfig =  eval(respText.fieldList)[0];
				form.getForm().setValues(fieldCodeConfig);
			}
//			var formValues = form.getForm().getValues();
//			fields[f_id] = formValues;
//			fieldsTemp[f_id] = formValues;
		},
		failure : function(response, options) {
			
		}
	});
	saveFieldFormValues(f_id);
};

function fieldCodeStore(q_id){
	var store = new Ext.data.Store({
        autoLoad: true,
        fields: ['id','code', 'name'],
        proxy: {
            type: 'ajax',
            url: '/sys-report/getFieldCodeStore',
            extraParams: {
            	Q_ID:q_id
            },
            reader: {
                type: 'json',
                root: 'list'
            }
        }
    });
	
	return store;
};

function fieldConfigChange(f_id,cellValue,row,col,rowCount,colCountFlag,dateType){
	var rightConfig = Ext.ComponentQuery.query('panel#rightConfig')[0];
	rightConfig.removeAll(true);
	rightConfig.setTitle("数据列配置");
	var column = {};
	var columnFlag = false;
	for(var i = 0; i < configColumns.length; i++){
		if(configColumns[i].F_ID == f_id ){
			column = configColumns[i];
			column.FIELD_TITLE = cellValue;
			columnFlag = true;
			break;
		}
	}
	if(!columnFlag){
		column = {
				F_ID:f_id,
				FIELD_TITLE:cellValue,
				FIELD_TYPE:dateType
		};
		configColumns.push(column);
	}
	rightConfig.add(initFieldConfig({
		 f_id:f_id,
         data: column,
         row:row,
         col:col,
         rowCount:rowCount,
         colCountFlag:colCountFlag
    }));
}

function configColumnsChange(f_id,code,text){
	var columnFlag = false;
	for(var i = 0; i < configColumns.length; i++){
		if(configColumns[i].F_ID == f_id ){
			configColumns[i].FIELD_CODE = code;
			configColumns[i].FIELD_TITLE = text;
			columnFlag = true;
			break;
		}
	}
	if(!columnFlag){
		configColumns.push({
			F_ID:f_id,
			FIELD_CODE:code,
			FIELD_TITLE:text
		});
	}
}

function fieldTitleChange(f_id,cellValue){
	var fieldTitle = Ext.ComponentQuery.query('textfield[name="FIELD_TITLE"]')[0];
	fieldTitle.setValue(cellValue);
	for(var i = 0; i < configColumns.length; i++){
		if(configColumns[i].F_ID == f_id ){
			configColumns[i].FIELD_TITLE = cellValue;
			break;
		}
	}
}
